package com.jf.blog_jf04.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jf.blog_jf04.bean.Blog;
import com.jf.blog_jf04.bean.Timeline;
import com.jf.blog_jf04.data.PageInfo;
import com.jf.blog_jf04.data.dto.BlogDto;
import com.jf.blog_jf04.data.dto.TimelineDto;
import com.jf.blog_jf04.mapper.TimelineMapper;
import com.jf.blog_jf04.service.TimelineService;
import com.jf.blog_jf04.utils.BeanConvertUtils;
import com.jf.blog_jf04.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class TimelineServiceImpl implements TimelineService {
    @Autowired
    private TimelineMapper timelineMapper;

    @Override
    public R getTimeline(Integer userinfoId) {
        List<Timeline> list = timelineMapper.getTimeline(userinfoId);
        return R.success(BeanConvertUtils.coptList(list, TimelineDto.class));
    }

    @Override
    public R getBlogPageByYear(int page, int userinfoId, String year) {
        PageInfo pageInfo = new PageInfo(page, 3);
        IPage<Blog> iPage = timelineMapper.getBlogByYearPage(pageInfo.setPage(), userinfoId, year);
        List<BlogDto> list = BeanConvertUtils.coptList(iPage.getRecords(), BlogDto.class);
        return R.success(pageInfo.setPageInfo(iPage.getTotal(), list));
    }

    @Override
    public R getEcharts(Integer userinfoId) {
        List<Timeline> pieList = timelineMapper.getBlogCountByCategory(userinfoId);
        List<TimelineDto> pieData = BeanConvertUtils.coptList(pieList, TimelineDto.class);
        List<Timeline> barList = timelineMapper.getTimeline(userinfoId);
        List<TimelineDto> barData = BeanConvertUtils.coptList(barList, TimelineDto.class);
        Map<String, List<TimelineDto>> map = new HashMap<>();
        map.put("pieData", pieData);
        map.put("barData", barData);
        return R.success(map);
    }
}
